home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / util / StackBuffer.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  92 lines

  1. /*    $Header: /usr/people/sam/fax/util/RCS/StackBuffer.c++,v 1.7 1994/02/28 14:24:19 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include "StackBuffer.h"
  26. #include <stdlib.h>
  27.  
  28. fxStackBuffer::fxStackBuffer(u_int grow)
  29. {
  30.     base = next = buf;
  31.     end = &buf[sizeof(buf)];
  32.     amountToGrowBy = grow ? grow : 500;
  33. }
  34.  
  35. fxStackBuffer::~fxStackBuffer()
  36. {
  37.     if (base != buf) delete base;
  38. }
  39.  
  40. fxStackBuffer::fxStackBuffer(const fxStackBuffer& other)
  41. {
  42.     u_int size = other.end - other.base;
  43.     u_int len = other.getLength();
  44.     if (size > sizeof(buf)) {
  45.     base = (char*) malloc(size);
  46.     } else {
  47.     base = &buf[0];
  48.     }
  49.     end = base + size;
  50.     next = base + len;
  51.     memcpy(base, other.base, len);
  52. }
  53.  
  54. void fxStackBuffer::addc(char c)
  55. {
  56.     if (next >= end) {
  57.     grow(amountToGrowBy);
  58.     }
  59.     *next++ = c;
  60. }
  61.  
  62. void fxStackBuffer::grow(u_int amount)
  63. {
  64.     // insure an acceptable amount of growth
  65.     if (amount < amountToGrowBy) amount = amountToGrowBy;
  66.  
  67.     // move data into larger piece of memory
  68.     u_int size = end - base;
  69.     u_int len = getLength();
  70.     u_int newSize = size + amount;
  71.     if (base == buf) {
  72.     base = (char*) malloc(newSize);
  73.     memcpy(base, buf, sizeof(buf));
  74.     } else {
  75.     base = (char*) realloc(base, newSize);
  76.     }
  77.  
  78.     // update position pointers
  79.     end = base + newSize;
  80.     next = base + len;
  81. }
  82.  
  83. void fxStackBuffer::put(char const* c, u_int len)
  84. {
  85.     u_int remainingSpace = end - next;
  86.     if  (len > remainingSpace) {
  87.     grow(len - remainingSpace);
  88.     }
  89.     memcpy(next, c, len);
  90.     next += len;
  91. }
  92.